package com.ygqh.baby.service.impl;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgPreSaleLogsMapper;
import com.ygqh.baby.po.YgPreSaleLogs;
import com.ygqh.baby.po.YgPreSaleLogsExample;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.service.YgPreSaleLogsService;
import com.ygqh.baby.service.YgProductService;

@Service
public class YgPreSaleLogsServiceImpl implements YgPreSaleLogsService{
	
	@Autowired
	private YgPreSaleLogsMapper ygPreSaleLogsMapper;
	@Autowired
	private YgProductService ygProductService;
	
	@Override
	public List<YgPreSaleLogs> find() {
		YgPreSaleLogsExample example = new YgPreSaleLogsExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygPreSaleLogsMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgPreSaleLogsExample example = new YgPreSaleLogsExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygPreSaleLogsMapper.countByExample(example);
	}

	@Override
	public YgPreSaleLogs findById(Long id) {
		return ygPreSaleLogsMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(YgPreSaleLogs ygPreSaleLogs) {
		return ygPreSaleLogsMapper.insertSelective(ygPreSaleLogs);
	}

	@Override
	public int update(YgPreSaleLogs ygPreSaleLogs) {
		return ygPreSaleLogsMapper.updateByPrimaryKeySelective(ygPreSaleLogs);
	}

	@Override
	public ResultSet<YgPreSaleLogs> search(QueryInfo queryInfo,String q) {
		List<YgPreSaleLogs>  list =  ygPreSaleLogsMapper.selectSuper(queryInfo, q);
		int count = ygPreSaleLogsMapper.countSuper(q);
		ResultSet<YgPreSaleLogs> resultSet = new ResultSet<YgPreSaleLogs>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgPreSaleLogsExample example = new YgPreSaleLogsExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgPreSaleLogs record = new YgPreSaleLogs();
		record.setStatus(DataStatus.Delete);

		return ygPreSaleLogsMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgPreSaleLogs ygPreSaleLogs) {
		int r = 0;
		if(ygPreSaleLogs.getId() != null) {
			r = this.update(ygPreSaleLogs);
		}else {
			r = this.save(ygPreSaleLogs);
		}
		return r;
	}

	@Override
	public int addPreSaleLogs(Long productId, Date preSaleDate, String handler, Date updateTime,String remark) {
		YgProduct product = ygProductService.findById(productId);
		YgPreSaleLogs ygPreSaleLogs = new YgPreSaleLogs(productId, product.getProductName(), product.getProductCode(), preSaleDate, handler, updateTime, remark);
		return this.save(ygPreSaleLogs);
	}

}

